package com.qf.smartdevice.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.smartdevice.cache.AreaCache;
import com.qf.smartdevice.cache.CategoriesCache;
import com.qf.smartdevice.mapper.DeviceMapper;
import com.qf.smartdevice.pojo.Area;
import com.qf.smartdevice.pojo.Categories;
import com.qf.smartdevice.pojo.Devices;
import com.qf.smartdevice.service.IDeviceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class DeviceServiceImpl implements IDeviceService {
    @Autowired
    private DeviceMapper deviceMapper;
    @Autowired
    private CategoriesCache categoriesCache;
    @Autowired
    private AreaCache areaCache;

    @Override
    public PageInfo<Devices> findAllDevice(int page, int limit) {
        PageHelper.startPage(page, limit);
        //这个数据只有自己的基本信息,分类和区域信息都没有
        List<Devices> devicesList = deviceMapper.findAllDevices();
        //从缓存中取到相关信息,设置到对象

        devicesList.forEach(devices -> {
            Long areaid = devices.getAreaid();//区域 id
            Long categoryid = devices.getCategoryid();//设备 id

            Area area = areaCache.getObj(areaid);//从缓存中获取指定的区域
            Categories categories = categoriesCache.getObj(categoryid);//从分类缓存中获取分类
            //将从缓存中获取到的区域和分类设置到当前遍历的对象上面
            devices.setArea(area);
            devices.setCategory(categories);

        });
        PageInfo<Devices> pageInfo=new PageInfo<>(devicesList);

        return pageInfo;
    }
}
